package com.ecoolex.chinadutyfree.retailer.core.manager;

import com.ecoolex.chinadutyfree.retailer.core.dao.DirectMailGoodsAuditDAO;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsAudit;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsAudit.Column;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsAuditExample;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsAuditExample.Criteria;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DirectMailGoodsAuditManager {
	
    @Autowired
    private DirectMailGoodsAuditDAO dao;
    
    public void create(Integer directMailGoodsId,Boolean result,String auditOpinion,String createUser) {
    	DirectMailGoodsAudit record = new DirectMailGoodsAudit();
    	record.setDirectMailGoodsId(directMailGoodsId);
    	record.setResult(result);
    	record.setAuditOpinion(auditOpinion);
    	record.setDeleted(DirectMailGoodsAudit.NOT_DELETED);
    	record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }
    
    public DirectMailGoodsAudit get(Integer auditId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(auditId, DirectMailGoodsAudit.NOT_DELETED);
    }
    
    public Pagination<DirectMailGoodsAudit> query(Sorting[] sorts,Page page){
    	DirectMailGoodsAuditExample example = new DirectMailGoodsAuditExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andDeletedEqualTo(DirectMailGoodsAudit.NOT_DELETED);
    	example.setOffset(page.getFirstIndex());
    	example.setRows(page.getPageSize());
    	example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
    	return Pagination.create(page, 
    		dao.countByExample(example),
    		() -> dao.selectByExample(example));
    }
}